home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / pdcurs21.zip / PRIVATE.ZIP / _CHADD.C < prev    next >
Text File  |  1992-11-21  |  7KB  |  213 lines

  1. #define        CURSES_LIBRARY  1
  2. #include <curses.h>
  3.  
  4. #ifndef        NDEBUG
  5. char *rcsid__chadd = "$Header: c:/curses/private/RCS/_chadd.c%v 2.0 1992/11/15 03:24:16 MH Rel $";
  6. #endif
  7.  
  8.  
  9.  
  10.  
  11. /*man-start*********************************************************************
  12.  
  13.   PDC_chadd()      - Low level; Put a character to a window
  14.  
  15.   PDCurses Description:
  16.         This is a private PDCurses function.
  17.  
  18.         This routine will insert the character 'c' at the current cursor
  19.         position in the passed window.
  20.  
  21.         If 'xlat' is TRUE, PDC_chadd() will handle things in a cooked
  22.         manner (tabs, newlines, carriage returns, etc).  If 'xlat' is
  23.         FALSE, the characters are simply output directly.
  24.  
  25.         If 'advance' is TRUE, PDC_chadd() will move the current cusor position
  26.         appropriately. The *addch functions call PDC_chadd() with noadvance TRUE,
  27.         while the *insch functions call PDC_chadd() with noadvance FALSE.
  28.  
  29.         The normal curses routines (non-raw-output-mode) call PDC_chadd()
  30.         with 'xlat' TRUE.
  31.  
  32.   PDCurses Return Value:
  33.         This function returns OK on success and ERR on error.
  34.  
  35.   PDCurses Errors:
  36.         It is an error to call this function with a NULL window pointer.
  37.  
  38.   Portability:
  39.         PDCurses        int PDC_chadd( WINDOW* win, chtype ch, bool xlat, bool noadvance );
  40.  
  41. **man-end**********************************************************************/
  42.  
  43. int    PDC_chadd(register WINDOW *win, chtype ch,bool xlat, bool advance)
  44. {
  45.        int     retval = ERR;
  46.        int     x;
  47.        int     y;
  48.        int     newx;
  49.        chtype  attr;
  50.        int     ts;
  51.  
  52.        if (win == (WINDOW *)NULL)
  53.                return( retval );
  54.  
  55.        x       = win->_curx;
  56.        y       = win->_cury;
  57.        ts      = win->_tabsize;
  58.  
  59. /* if the incoming character doesn't have its own attribute
  60.    then        use the current attributes for the window.
  61.    if the incoming character has attributes but        not a colour
  62.    component, or the attributes        to the current attributes
  63.    for the window.
  64.    if the incoming character has a colour component use        the
  65.    attributes solely from the incoming character */
  66.  
  67.        if ((ch & A_ATTRIBUTES) == 0)
  68.           attr = win->_attrs;
  69.        else
  70.           if ((ch & A_COLOR) == 0)
  71.              attr = (ch & A_ATTRIBUTES) | win->_attrs;
  72.           else
  73.              attr = (ch & A_ATTRIBUTES);
  74.  
  75.        ch      = (ch & A_CHARTEXT);
  76.  
  77.        if ((y > win->_maxy) ||
  78.            (x > win->_maxx) ||
  79.            (y < 0) ||
  80.            (x < 0))
  81.        {
  82.                return( retval );
  83.        }
  84.  
  85.        if (xlat)
  86.        {
  87.                switch (ch) {
  88.                case '\t':
  89.                        for (newx = ((x / ts) + 1) * ts; x < newx; x++)
  90.                        {
  91.                                if (waddch(win, ' ') == ERR)
  92.                                {
  93.                                        return( retval );
  94.                                }
  95.                                /*
  96.                                 * if tab to next line
  97.                                 */
  98.                                if (win->_curx == 0)
  99.                                {
  100.                                        /*
  101.                                         * exit the loop
  102.                                         */
  103.                                        return( OK );
  104.                                }
  105.                        }
  106.                        return( OK );
  107.  
  108.                case '\n':
  109.                        if (_cursvar.autocr && !(_cursvar.raw_out))
  110.                        {
  111.                                /*
  112.                                 * if lf -> crlf
  113.                                 */
  114.                                x = 0;
  115.                        }
  116.                        wclrtoeol( win );
  117.                        if ((y = PDC_newline(win, y)) < 0)
  118.                                return( retval );
  119.                        if (advance)
  120.                          {
  121.                           win->_cury = y;
  122.                           win->_curx = x;
  123.                          }
  124.                        return( OK );
  125.  
  126.                case '\r':
  127.                        if (advance)
  128.                           win->_curx = x = 0;
  129.                        return( OK );
  130.  
  131.                case '\b':
  132.                        if (--x < 0)
  133.                        {
  134.                                /*
  135.                                 * no back over left margin
  136.                                 */
  137.                                x = 0;
  138.                        }
  139.                        if (advance)
  140.                           win->_curx = x;
  141.                        return( OK );
  142.  
  143.                case 0x7f:
  144.                        if (waddch(win, '^') == ERR)
  145.                        {
  146.                                return( retval );
  147.                        }
  148.                        retval = waddch(win, '?');
  149.                        return( retval );
  150.  
  151.                default:
  152.                        break;
  153.                }               /* switch */
  154.  
  155.                if (ch < ' ')
  156.                {
  157.                        /*
  158.                         * handle control chars
  159.                         */
  160.                        if (waddch(win, '^') == ERR)
  161.                                return( retval );
  162.  
  163.                        retval = (waddch(win, ch + '@'));
  164.                        return( retval );
  165.                }
  166.        }
  167.  
  168.        /*
  169.         *      Add the attribute back into the character.
  170.         */
  171.        ch      |= attr;
  172.        if (win->_y[y][x] !=    ch)
  173.        {
  174.                /*
  175.                 * only if data change
  176.                 */
  177.                if (win->_firstch[y] == _NO_CHANGE)
  178.                {
  179.                        win->_firstch[y] = win->_lastch[y] = x;
  180.                }
  181.                else
  182.                {
  183.                        if (x < win->_firstch[y])
  184.                        {
  185.                                win->_firstch[y] = x;
  186.                        }
  187.                        else
  188.                        {
  189.                                if (x > win->_lastch[y])
  190.                                {
  191.                                        win->_lastch[y] = x;
  192.                                }
  193.                        }
  194.                }
  195.        }
  196.        win->_y[y][x++] = ch;
  197.        if (x >= win->_maxx)
  198.        {
  199.                /*
  200.                 * wrap around test
  201.                 */
  202.                x = 0;
  203.                if ((y = PDC_newline(win, y)) < 0)
  204.                        return( retval );
  205.        }
  206.        if (advance)
  207.          {
  208.           win->_curx = x;
  209.           win->_cury = y;
  210.          }
  211.        return( OK );
  212. }
  213.